package com.alfredo.android.appcaja;

import com.alfredo.android.appcaja.constants.Keys;
import com.alfredo.android.appcaja.constants.RequesCodes;
import com.alfredo.android.appcaja.model.InformeFinal;
import com.alfredo.android.appcaja.model.Orden;
import com.alfredo.android.appcaja.util.Utils;
import com.luminous.pick.Action;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class InformeFinalActivity extends Activity 
{
	private Orden orden;
	private String[] urlsFotos = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_informe_final);
		
		Bundle extras = getIntent().getExtras();
		orden = (Orden) extras.getSerializable(Keys.ORDEN);
		
		EditText etIFinalCgi = (EditText) findViewById(R.id.etIFinalCgi);
		etIFinalCgi.setText(String.valueOf(orden.getCgi()));
		
		EditText etIFinalNExpediente = (EditText) findViewById(R.id.etIFinalNExpediente);
		etIFinalNExpediente.setText(String.valueOf(orden.getNroExpediente()));
		
		EditText etIFinalCausanteBeneficiario = (EditText) findViewById(R.id.etIFinalCausanteBeneficiario);
		etIFinalCausanteBeneficiario.setText(orden.getCausanteBeneficiario());
		
		Button btnIFinalAdjuntarFotos = (Button) findViewById(R.id.btnIFinalAdjuntarFotos);
		btnIFinalAdjuntarFotos.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				Intent pickIntent = new Intent(Action.ACTION_MULTIPLE_PICK);
				startActivityForResult(pickIntent, RequesCodes.PICK_MULTIPLE_PATHS);
			}
		});
		
		Button btnIFinalEnviar = (Button) findViewById(R.id.btnIFinalEnviar);
		btnIFinalEnviar.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				enviarEmail();
			}
		});
	}
	
	private InformeFinal cargarInformeFinal()
	{
		InformeFinal informeFinal = new InformeFinal();
		
		EditText etIFinalObservaciones = (EditText) findViewById(R.id.etIFinalObservaciones);
		informeFinal.setComentarios(etIFinalObservaciones.getText().toString());
		
		return informeFinal;
	}
	
	private void enviarEmail()
	{
		InformeFinal informeFinal = cargarInformeFinal();
		orden.setInformeFinal(informeFinal);
		StringBuilder sb = new StringBuilder();
		sb.append("<b>Comentarios:</b> <br><br>");
		sb.append(orden.getInformeFinal().getComentarios().replace("\n", "<br>"));
		Utils.enviarEmail(this, "Informe Final", orden,sb.toString(), urlsFotos);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) 
	{
		super.onActivityResult(requestCode, resultCode, data);
		
		if (requestCode == RequesCodes.PICK_SINGLE_PATH && resultCode == Activity.RESULT_OK) 
		{
            String single_path = data.getStringExtra("single_path");
            urlsFotos = new String[]{single_path};
        }
		else if (requestCode == RequesCodes.PICK_MULTIPLE_PATHS && resultCode == Activity.RESULT_OK) 
        {
            String[] all_path = data.getStringArrayExtra("all_path");
            urlsFotos = all_path;
        }
	}
}
